<template>
  <div>
    <a-modal
      :title="title"
      :width="800"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
    >
      <a-spin :spinning="confirmLoading">
        <a-form-model
          layout="horizontal"
          :form="form"
          ref="form"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          :model="queryParam"
          class="form_model"
          :rules="validatorRules"
        >
          <a-form-item label="物品名称" required prop="itemName">
            <a-input v-model="selectGoodItem.itemName" placeholder="请选择要领用的物品" disabled />
            <a-button type="primary" @click="handleSelect()">选择要领用的物品</a-button>
          </a-form-item>
          <a-form-item label="领用人" has-feedback prop="optUserName">
            <a-select placeholder="请选择领用人" @change="selectReceiver" allowClear v-model="queryParam.optUserName">
              <a-select-option :value="item.title" v-for="item in dataSource" :key="item.value">
                {{ item.text }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-form-model>
      </a-spin>
    </a-modal>
    <OutWareHouseGoodModal
      ref="modalForm"
      @ok="modalFormOk(this.queryParam)"
      @selectGood="selectGood"
    ></OutWareHouseGoodModal>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, httpAction, postAction } from '@api/manage'
import OutWareHouseGoodModal from './OutWareHouseGoodModal'

export default {
  name: 'OutWareHouseModal',
  mixins: [JeecgListMixin],
  data() {
    return {
      treeValue: '',
      treeExpandedKeys: [],
      selectGoodItem: {},
      title: '操作',
      visible: false,
      model: {},
      layout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 14 }
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      url: {
        list: '/sys/dict/getDictItems/sys_user,realname,id'
        // edit: "/test/jeecgDemo/edit",
        // post: '/outWarehouseCtrl/add'
      },
      validatorRules: {
        itemName: [{ required: true, message: '请输入职务编码' }],
        optUserName: [{ required: true, message: '请输入职务编码' }]
      }
    }
  },
  components: {
    OutWareHouseGoodModal
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$refs.form.resetFields()
      this.$emit('close')
      this.visible = false
    },
    handleOk(e) {
      e.preventDefault()
      console.log('出库参数', this.queryParam)
      if (!this.queryParam.itemName) {
        this.$message.warning('请选择物品!')
      } else if (!this.queryParam.optUserName) {
        this.$message.warning('请选择领用人!')
      } else {
        postAction('/outWarehouseCtrl/add', this.queryParam)
          .then(res => {
            console.log('出库响应', res)
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok')
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.confirmLoading = false
            this.close()
            this.queryParam = {}
            this.selectGoodItem = {}
          })
      }
    },
    handleCancel() {
      this.queryParam = {}
      this.selectGoodItem = {}
      this.close()
    },
    //获取领用名和领用者id
    selectReceiver(value, option) {
      this.queryParam.optUserName = value
      this.queryParam.optUserId = option.key
    },
    //获取物品名和物品id
    selectGood(value) {
      console.log('接收子组件数据', value)
      this.selectGoodItem = value
      this.queryParam.itemName = value.itemName
      this.queryParam.itemId = value.itemId
    }
  }
}
</script>

<style scoped>
.form_model {
  display: flex;
  flex-wrap: wrap;
}
.ant-form-item {
  width: 50%;
}

.ant-modal {
  margin-top: 100px !important;
}

.ant-modal-content {
  margin-top: 100px !important;
}
</style>
